#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    this->resize(1024,768);
    this->setMaximumSize(1024,768);
    this->setMinimumSize(1024,768);

    _serial_set_param  = new SERIAL_SET_PARAM;

    serial_port = new QSerialPort;
    connect(serial_port,SIGNAL(readyRead()),this,SLOT(serial_receive_info()));





    QStringList menuBtnStrList;
    menuBtnStrList<<"数据解析"<<"设置串口"<<"报文监控"<<"协议配置"<<"关于"<<"退出";

    QWidget * menuWidget = new QWidget (this);
    menuWidget->setStyleSheet("background-color:#4D4D4D;");//border:1px solid red;#4D4D4D
    menuWidget->resize(this->width(),45);
    QHBoxLayout *menuTopLayout = new QHBoxLayout(menuWidget);
    menuTopLayout ->setMargin(0);
    menuTopLayout ->setSpacing(30);
    menuTopLayout->addStretch(1);
    ui_design = new uidesign;
    for(int i = 0; i< MENU_BTN_CNT; i++)
    {
        QPushButton * btn = new QPushButton(menuWidget);
        btn->setText(menuBtnStrList.at(i));
        menuTopLayout->addWidget(btn,10);
        menu_btn_list.append(btn);
        ui_design->setBtnQss(btn, "#4E6D8C", "#FFFFFF", "#34495E", "#F0F0F0", "#2D3E50", "#B8C6D1");
        if(i == MENU_BTN_CNT -2)
        {
            menuTopLayout->addStretch(3);
        }
        connect(btn,SIGNAL(released()),this,SLOT(menu_btn_handle()),Qt::AutoConnection);//


    }
    menuTopLayout->addStretch(1);


    serial_analyse_data = new SerialAnalyseData(this);
    serial_analyse_data->setObjectName("serial_analyse_data");
    serial_analyse_data->setStyleSheet("background-color:#C3C7BB;");
    serial_analyse_data->resize(this->width(),this->height()-45);
    serial_analyse_data->move(0,45);
    serial_analyse_data->hide();
    menu_widget_list.append(serial_analyse_data);
    connect(serial_analyse_data,SIGNAL(serial_write_signal(QByteArray)),this,SLOT(serial_write_handle(QByteArray)),Qt::AutoConnection);//






    serial_protocol = new SerialProtocol(this);
    serial_protocol->setObjectName("serial_protocol");
    serial_protocol->setStyleSheet("background-color:#C3C7BB;");
    serial_protocol->resize(this->width(),this->height()-45);
    serial_protocol->move(0,45);
    serial_protocol->hide();
    menu_widget_list.append(serial_protocol);





    set_serial_param = new SetSerialParam(this,_serial_set_param);
    set_serial_param->setObjectName("set_serial_param");
    set_serial_param->setStyleSheet("background-color:#C3C7BB;");
    set_serial_param->resize(this->width(),this->height()-45);
    set_serial_param->move(0,45);
    set_serial_param->hide();
    menu_widget_list.append(set_serial_param);
    connect(set_serial_param,SIGNAL(serial_open_signal()),this,SLOT(serial_open_handle()),Qt::AutoConnection);//
    connect(set_serial_param,SIGNAL(serial_close_signal()),this,SLOT(serial_close_handle()),Qt::AutoConnection);//




    serial_debug  = new SerialDebug(this);
    serial_debug->setObjectName("serial_debug");
    serial_debug->setStyleSheet("background-color:#C3C7BB;");
    serial_debug->resize(this->width(),this->height()-45);
    serial_debug->move(0,45);
    serial_debug->hide();
    menu_widget_list.append(serial_debug);

}

void MainWindow::menu_btn_handle()
{
    QPushButton* btn = qobject_cast<QPushButton*>(sender());

    if(btn == menu_btn_list.at(0))
    {
        for(int i = 0; i<menu_widget_list.count();i++)
        {
            menu_widget_list.at(i)->hide();
            if(menu_widget_list.at(i)->objectName()=="serial_analyse_data")
            {
                menu_widget_list.at(i)->show();
            }
        }
    }
    else if(btn == menu_btn_list.at(1))
    {
         for(int i = 0; i<menu_widget_list.count();i++)
         {
             menu_widget_list.at(i)->hide();
             if(menu_widget_list.at(i)->objectName()=="set_serial_param")
             {
                 menu_widget_list.at(i)->show();
             }
         }

    }
    else if(btn == menu_btn_list.at(2))//报文监控响应
    {
        for(int i = 0; i<menu_widget_list.count();i++)
        {
            menu_widget_list.at(i)->hide();
            if(menu_widget_list.at(i)->objectName()=="serial_debug")
            {
                menu_widget_list.at(i)->show();
            }
        }

    }
    else if(btn == menu_btn_list.at(3))
    {
        for(int i = 0; i<menu_widget_list.count();i++)
        {
            menu_widget_list.at(i)->hide();
            if(menu_widget_list.at(i)->objectName()=="serial_protocol")
            {
                menu_widget_list.at(i)->show();
            }
        }
    }
    else if(btn == menu_btn_list.at(MENU_BTN_CNT -1))
    {
        this->close();

    }

}


//接收数据槽函数
void MainWindow::serial_receive_info()
{
    QByteArray info = serial_port->readAll();
    serial_debug->serial_debug_rec_data(info);
    QByteArray hexData = info.toHex();
    qDebug()<<"------"<<hexData;
    if(hexData == "0x10000")
    {
    //do something
    }
    else if(hexData  == "0x100001")
    {
    //do something
    }



}

void MainWindow::serial_open_handle()
{
    //获取串口参数
    set_serial_param->get_serial_param();
    serial_port->setBaudRate(_serial_set_param->m_serial_param_baud,QSerialPort::AllDirections);//设置波特率和读写方向
    serial_port->setDataBits(_serial_set_param->m_serial_param_data_bit);		//数据位为8位
    serial_port->setFlowControl(QSerialPort::NoFlowControl);//无流控制
    serial_port->setParity(_serial_set_param->m_serial_param_check_bit);	//无校验位
    serial_port->setStopBits(_serial_set_param->m_serial_param_stop_bit); //一位停止位
    serial_port->setPortName(_serial_set_param->serial_port_name);

    if(!serial_port->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
    {
        qDebug()<<_serial_set_param->serial_port_name<<"打开失败!";
        return;
    }
}
void MainWindow::serial_close_handle()
{

    if(serial_port->isOpen())//如果串口已经打开了 先给他关闭了
    {
        serial_port->clear();
        serial_port->close();
    }
}


void MainWindow::serial_write_handle(QByteArray buf)
{
    serial_debug->serial_debug_send_data(buf);
   serial_port->write(buf);
}






MainWindow::~MainWindow()
{
    if(ui_design != nullptr)
    {
        delete  ui_design;
        ui_design = nullptr;
    }
    if(_serial_set_param != nullptr)
    {
        delete _serial_set_param;
        _serial_set_param = nullptr;
    }
    if(serial_port != nullptr)
    {
        delete serial_port;
        serial_port = nullptr;
    }
    delete ui;
}
